package AAAA.web;


import AAAA.exception.MoneyNotEnoughException;
import AAAA.exception.transferException;
import AAAA.service.AccountService;
import AAAA.service.Impl.AccountServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
@WebServlet("/transfer")
public class AccountServlet extends HttpServlet {

    private AccountService accountService = new AccountServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fromActno = req.getParameter("fromActno");
        String toActno = req.getParameter("toActno");
        String money1 = req.getParameter("money");
        double money = Double.parseDouble(money1);

        //调用service层的转账方法完成转账（调业务层）
        try {
            accountService.transfer(fromActno,toActno,money);
            //调用视图层view完成展示结果
            resp.sendRedirect(req.getContextPath()+"/success.html");
        } catch (MoneyNotEnoughException e) {
           resp.sendRedirect(req.getContextPath()+"/error1.html");
        } catch (transferException e) {
            resp.sendRedirect(req.getContextPath()+"/error2.html");
        }




    }
}
